package org.wattdepot.cli;

import java.util.ArrayList;
import java.util.List;
import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.client.WattDepotClientException;
import org.wattdepot.resource.source.jaxb.Source;
import org.wattdepot.resource.property.jaxb.Property;
import org.wattdepot.util.tstamp.Tstamp;
import javax.xml.datatype.XMLGregorianCalendar;

/**
 * Generates the total power generated by all non-virtual sources associated 
 * with this source that use the specified fuel type.
 * Input 1:  totalpower SIM_IPP 2009-10-26T18:15:00.000-10:00 fueltype waste
 * Input 2:  totalpower SIM_KAHE 2009-10-26T18:15:00.000-10:00 fueltype waste
 * @author Edwin Lim, Wing Ma Cheung
 *
 */
public class TotalPowerFuelType extends CommandLineInterface implements WattDepotCliCommand {
  /**
   * Calls the method that would print the summarized list of a single power plant.
   * @param args Strings of input arguments
   * @return output a collection of information about one source
   */
  @Override
  public String doCommand(List<String> args) {
    String input = args.get(0);
    String fuelType = args.get(3);
    String timeStamp = args.get(1);
    double total = 0.0;
    String output = "";
    Source source = null;
    XMLGregorianCalendar now = null;

    // array list that will hold the subsources as a string
    List<String> subsources = new ArrayList<String>();
    // Check if the input source is valid
    try {
      source = searchSource(input);
    }
    catch (WattDepotClientException eCli) {
      eCli.printStackTrace();
    }
    
    if (source == null) {
      return sourceNotFound;
    }
    
    try {
      now = Tstamp.makeTimestamp(timeStamp);
    }
    catch (Exception e) {
      System.out.println("should never happen.");
    }

    //check if the source is virtual 
    try {
      if (source.isVirtual()) {
        // check if any subsource is available
        if (source.isSetSubSources()) {       
          for (String subsource : source.getSubSources().getHref()) {
            // finds the substring that starts with SIM
            int i = subsource.indexOf("SIM");
            subsources.add(subsource.substring(i));
          }    
        }   
        // Convert each string source into a source class
        for (String test : subsources) {
          try {
            source = searchSource(test);
          }
          catch (WattDepotClientException eCli) {
            eCli.printStackTrace();
          }
          // Finds the subsources.
          if (source.isSetProperties()) {
            // Loop each of the subsource properties
            for (Property property : source.getProperties().getProperty()) {
              if (property.getValue().equalsIgnoreCase(fuelType)) {
                //add the values of each subsource with the same fuel type
                total = total + client.getPowerGenerated(source.getName(), now);
                output = Double.toString(total);
              } // end if
            } // end properties for
          } // end properties if
        } // end subsource for
        if (total == 0.0) {
          //checks if the the specified input is avaialable
          output = "The specified fuel type was not available.";
        }
      }
      // is source is non virtual
      else {
        System.out.println(source.getProperty("fuelType"));
        if (source.getProperty("fuelType").equalsIgnoreCase(fuelType)) {
          System.out.println(source.getProperty("fuelType"));
          total = client.getPowerGenerated(source.getName(), now);
          output = Double.toString(total);
        }
        else {
          output = "There is no non-virtual source with the specified fuel type";
        }
      }
    }
    catch (NotAuthorizedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (ResourceNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (BadXmlException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (MiscClientException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return output;
  }
}
